package cron

import (
	"fmt"
	"gin_blog/models"
	"github.com/robfig/cron/v3"
	"log"
	"time"
)

func Cron() {
	log.Println("Starting...")

	c :=cron.New(cron.WithSeconds()) // 创建定时任务 秒

	//执行定时任务
	spec := "0 0 0 * * ? " //每天00:00:00执行
	_,err :=c.AddFunc(spec, func() {
		log.Println("Run models.cleanAllTag...")
		_, _ = models.CleanAllTag()
	})
	_,err =c.AddFunc(spec, func() {
		log.Println("Run models.CleanAllArticle...")
		models.CleanAllArticle()
	})

	_,err = c.AddFunc(spec, print1)
	if err!=nil{
		fmt.Println(err)
	}

	c.Start()
	defer c.Stop()
	t1 :=time.NewTimer(time.Second*10)
	for  {
		select {
		case <-t1.C:
			t1.Reset(time.Second*10)
		}
	}
}
//执行函数
func print1()  {
	fmt.Printf("每天00:00:00执行一次cron,%v\n",time.Now().Format("2006-01-02 15:04:05"))
}